package com.anlu.base.jdk.jdk8.functions;

import java.util.function.Function;

/**
 * <p>
 * Description:Function<T, R>
 *
 * 抽象方法：R apply(T t)
 * 用途：表示一个接受一个输入参数并返回一个结果的函数。
 * 使用场景：当需要对输入参数进行处理，并返回一个结果时，可以使用Function。
 * <p>
 * <p>
 *
 * @author anlu
 * @date 2024/9/24 14:11
 */
public class SalaryCalculator
{
    public static void main(String[] args)
    {
        Employee employee = new Employee("John", 40, 25.0);
        double salary = calculateSalary(employee, emp -> emp.getHoursWorked() * emp.getHourlyRate());
        System.out.println("Salary: " + salary);
    }

    public static double calculateSalary(Employee employee, Function<Employee, Double> salaryFunction)
    {
        return salaryFunction.apply(employee);
    }
}

class Employee {
    private String name;
    private int hoursWorked;
    private double hourlyRate;

    public Employee(String name, int hoursWorked, double hourlyRate) {
        this.name = name;
        this.hoursWorked = hoursWorked;
        this.hourlyRate = hourlyRate;
    }

    public String getName() {
        return name;
    }

    public int getHoursWorked() {
        return hoursWorked;
    }

    public double getHourlyRate() {
        return hourlyRate;
    }
}
